from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class SearchPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.search_box_locator = (By.ID, "search-box")
        self.search_button_locator = (By.ID, "search-button")
        self.search_results_locator = (By.ID, "search-results")
        self.product_name_locator = (By.CLASS_NAME, "product-name")
        self.product_price_locator = (By.CLASS_NAME, "product-price")
        self.no_results_message_locator = (By.ID, "no-results-message")

    def navigate(self):
        self.driver.get('http://example.com/search')

    def enter_search_query(self, query):
        search_box = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.search_box_locator)
        )
        search_box.clear()
        search_box.send_keys(query)

    def click_search_button(self):
        search_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.search_button_locator)
        )
        search_button.click()

    def get_search_results(self):
        search_results = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.search_results_locator)
        )
        return search_results

    def get_product_names(self):
        product_names = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.product_name_locator)
        )
        return [name.text for name in product_names]

    def get_product_prices(self):
        product_prices = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.product_price_locator)
        )
        return [price.text for price in product_prices]

    def get_no_results_message(self):
        no_results_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.no_results_message_locator)
        )
        return no_results_message.text